package com.ellricky.baselib.ui.clue

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import com.andr.common.http.callback.BaseCallback
import com.andr.common.ui.adapter.BaseRecyclerAdapter
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.widget.refresh.RefreshFlag
import com.andr.common.widget.refresh.RvRefreshListener
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.adapter.clue.ClueAdapter
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.entity.clue.MyClueEntity
import com.ellricky.baselib.entity.new.MyClueData
import com.ellricky.baselib.entity.new.MyClueList
import com.ellricky.baselib.http.Api.API_CLUE_POOL_LIST
import com.ellricky.baselib.http.callback.EListCallback
import com.ellricky.baselib.ui.base.BaseRefreshFragment
import com.ellricky.baselib.util.*

/**
 * 线索池
 * 回收
 */
class CluePoolFragment : BaseRefreshFragment<Void, BasePresenter<Void>, MyClueData>(),
    RvRefreshListener, AdapterView.OnItemClickListener {

    private var clueNum = "0"
    private var keyword = ""

    private val state: String by lazy {
        arguments?.getString("state") ?: ""
    }

    /**
     * 28待办理，29有意向，30体验课,31回收站,32已完成）
     */
    companion object {
        fun newInstance(state: String): CluePoolFragment {
            val fragment = CluePoolFragment()
            val bundle = Bundle().apply {
                putString("state", state)
            }
            fragment.arguments = bundle
            return fragment
        }
    }


    override fun initUIView(view: View?) {
        super.initUIView(view)
        hideToolBar()
    }

    override fun setRefreshListener() {
        rvView.init(this, RefreshFlag.REFRESH)
    }

    override fun getResponseCallback(): BaseCallback? {
        return null
    }

    override fun getResponseCallback2(): NewEBaseCallback {
        return object : NewECallback<MyClueList>(mContext) {
            override fun onSuccess(data: MyClueList, msg: String?) {
                val activity = activity as ClueLibActivity
                activity.refreshClubNum(data.clue_num)
                clueNum = data.clue_num
                dataSource = data.data_list
                rvView.onLoadFinish(data.data_list, refresh)
            }
        }
    }

    override fun getRvAdapter(): BaseRecyclerAdapter<MyClueData>? {
        return ClueAdapter()
    }

    override fun getUrlPath(): String {
        return ""
    }

    override fun initParamMap() {
        super.initParamMap()
        paramMap["action"] = "headClueList"
        paramMap["school_id"] = LocalCacheUtil.getSchoolId()
        paramMap["eid"] = LocalCacheUtil.getUid()
        paramMap["clue_state"] = state
        paramMap["search_key"] = keyword
    }

    fun search(keyword:String){
        this.keyword = keyword
        rvView.loadContent(true)
    }

    fun resetKeyword(){
        this.keyword = ""
    }

    fun getCurNumber(): String {
        return clueNum
    }

    override fun onItemClick(adapterView: AdapterView<*>?, view: View?, i: Int, l: Long) {
        val id = adapter[i].clue_id
        jump<CluePoolDetailActivity> {
            putExtra(BaseConstant.KEY_ID, id)
        }
    }
}
